package com.pengpeng.flash.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pengpeng.flash.common.BaseContext;
import com.pengpeng.flash.common.R;
import com.pengpeng.flash.entity.AddressBook;
import com.pengpeng.flash.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;

    /**
     * 查询当前用户所有收货地址
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> addressList(){
        //先查询当前登录用户的id
        long userId = BaseContext.getCurrentId();
        //根据用户id查询该用户所有的收货地址
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,userId);
        queryWrapper.orderByAsc(AddressBook::getUpdateTime);
        List<AddressBook> addressBooks = addressBookService.list(queryWrapper);
        if (addressBooks != null){
            return R.success(addressBooks);
        }
        return R.error("查询失败");
    }

    /**
     * 新增收货地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> saveAddress(@RequestBody AddressBook addressBook){
        //获取当前登录用户的id
        long userId = BaseContext.getCurrentId();
        //设置地址中的id属性并保存
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("地址新增成功");
    }

    /**
     * 修改默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> defaultAddress(@RequestBody AddressBook addressBook){
        //先获取当前登录用户的id
        long userId = BaseContext.getCurrentId();
        //sql：UPDATE address_book SET is_default=0 WHERE (user_id = ?)
        //利用updateWrapper先把当前用户所有的地址的‘默认地址’字段都设置为非默认
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId,userId);
        updateWrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(updateWrapper);

        //sql：UPDATE address_book SET is_default=1 WHERE id=?
        //设置对应id的地址的is_default字段为1
        addressBook.setIsDefault(1);
        //这里因为入参的AddressBook对象中只有is_default字段被赋值，所以就只更改这个字段
        addressBookService.updateById(addressBook);
        return R.success("默认地址修改成功");
    }

    /**
     * 根据id查询对应地址信息，用于回显到修改页面
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> addressFindOne(@PathVariable("id")String id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null){
            return R.success(addressBook);
        }
        return R.error("没有找到对应地址");
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> updateAddressApi(@RequestBody AddressBook addressBook){
        addressBookService.updateById(addressBook);
        return R.success("地址修改成功");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAddress(@RequestParam String ids){
        addressBookService.removeById(ids);
        return R.success("地址删除成功");
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress(){
        //查询当前登录用户的默认地址
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        queryWrapper.eq(AddressBook::getIsDefault,1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (addressBook != null){
            return R.success(addressBook);
        }
        return R.error("没有找到对应地址");
    }
}
